package model

import "time"

// ValidationResult 验证结果
type ValidationResult struct {
	ID           int64             `json:"id"`
	IsValid      bool              `json:"is_valid"`
	ErrorType    string            `json:"error_type,omitempty"`
	ErrorMessage string            `json:"error_message,omitempty"`
	Severity     string            `json:"severity,omitempty"` // error, warning, info
	SQL          string            `json:"sql,omitempty"`
	CheckType    string            `json:"check_type,omitempty"`
	Details      ValidationDetails `json:"details,omitempty"`
	CreatedAt    time.Time         `json:"created_at"`
}

// ValidationDetails 验证详细信息
type ValidationDetails struct {
	MissingComments []string `json:"missing_comments,omitempty"`
	ExistingTables  []string `json:"existing_tables,omitempty"`
	ExistingColumns []string `json:"existing_columns,omitempty"`
	InvalidObjects  []string `json:"invalid_objects,omitempty"`
	Warnings        []string `json:"warnings,omitempty"`
	Suggestions     []string `json:"suggestions,omitempty"`
}

// ValidationRequest 验证请求
type ValidationRequest struct {
	DDL                  string   `json:"ddl"`
	Rollback             string   `json:"rollback,omitempty"`
	AllowedSchemas       []string `json:"allowed_schemas,omitempty"`
	AllowedTables        []string `json:"allowed_tables,omitempty"`
	AutoGenerateRollback bool     `json:"auto_generate_rollback,omitempty"`
	EnableExternalCheck  bool     `json:"enable_external_check,omitempty"`
	ExternalAPIURL       string   `json:"external_api_url,omitempty"`
}

// ValidationResponse 验证响应
type ValidationResponse struct {
	RequestID           string               `json:"request_id"`
	IsValid             bool                 `json:"is_valid"`
	DDLResults          []ValidationResult   `json:"ddl_results"`
	RollbackResults     []ValidationResult   `json:"rollback_results"`
	Summary             ValidationSummary    `json:"summary"`
	GeneratedRollback   string               `json:"generated_rollback,omitempty"`
	ExternalCheckResult *ExternalCheckResult `json:"external_check_result,omitempty"`
	ProcessedAt         time.Time            `json:"processed_at"`
}

// ValidationSummary 验证摘要
type ValidationSummary struct {
	TotalChecks     int    `json:"total_checks"`
	PassedChecks    int    `json:"passed_checks"`
	FailedChecks    int    `json:"failed_checks"`
	WarningChecks   int    `json:"warning_checks"`
	ErrorCount      int    `json:"error_count"`
	WarningCount    int    `json:"warning_count"`
	HasDDL          bool   `json:"has_ddl"`
	HasRollback     bool   `json:"has_rollback"`
	IsDDLValid      bool   `json:"is_ddl_valid"`
	IsRollbackValid bool   `json:"is_rollback_valid"`
	OverallStatus   string `json:"overall_status"` // valid, invalid, warning
}

// ExternalCheckResult 外部API检查结果
type ExternalCheckResult struct {
	IsValid     bool                   `json:"is_valid"`
	Message     string                 `json:"message"`
	Details     map[string]interface{} `json:"details,omitempty"`
	APIResponse map[string]interface{} `json:"api_response,omitempty"`
	Error       string                 `json:"error,omitempty"`
}

// TableInfoWithSchema 带Schema的表信息
type TableInfoWithSchema struct {
	SchemaName string `json:"schema_name"`
	TableName  string `json:"table_name"`
	FullName   string `json:"full_name"`
}

// ColumnInfoWithSchema 带Schema的列信息
type ColumnInfoWithSchema struct {
	SchemaName string `json:"schema_name"`
	TableName  string `json:"table_name"`
	ColumnName string `json:"column_name"`
	FullName   string `json:"full_name"`
}

// DDLRow 表示DDL表格的一行数据
type DDLRow struct {
	Schema         string `json:"schema"`
	Table          string `json:"table"`
	Script         string `json:"script"`
	RollbackScript string `json:"rollback_script,omitempty"`
}

// DDLValidationResult DDL验证结果详情
type DDLValidationResult struct {
	OriginalSQL        string                `json:"original_sql"`
	ParsedSQL          interface{}           `json:"parsed_sql,omitempty"`
	SyntaxErrors       []string              `json:"syntax_errors"`
	CommentErrors      []string              `json:"comment_errors"`
	ExistenceErrors    []string              `json:"existence_errors"`
	ScopeErrors        []string              `json:"scope_errors"`
	RelatedObjects     []TableInfoWithSchema `json:"related_objects"`
	ValidationWarnings []string              `json:"validation_warnings"`
	IsValid            bool                  `json:"is_valid"`
	Suggestions        []string              `json:"suggestions"`
}
